package com.lanou3g.mynewstest.module.topic;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.lanou3g.mynewstest.R;
import com.lanou3g.mynewstest.base.MyBaseActivity;
import com.lanou3g.mynewstest.beans.TopicDetailBeans;
import com.lanou3g.mynewstest.tools.CircleImageView;
import com.lanou3g.mynewstest.tools.MyRequestQueue;


/**
 * Created by dllo on 16/1/25.
 */
public class TopicDetailActivity extends MyBaseActivity implements View.OnClickListener {
    private Handler handler;
    private ListView listView;
    private CircleImageView circleImageView;
    private NetworkImageView networkImageView;
    private TextView alias, description, title, concernCount, questionCount;
    private ImageView imageView,open;
    private TopicDetailBeans beans;
    private TopicDetailLatestAdapter adapter;
    private TextView hot, latest;

    private boolean state=true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_topic_detail);
        listView = (ListView) findViewById(R.id.topic_detail_lv);

        final View headView = LayoutInflater.from(getBaseContext()).inflate(R.layout.item_topic_detail_head, null);
        circleImageView = (CircleImageView) headView.findViewById(R.id.topic_head_net);
        networkImageView = (NetworkImageView) headView.findViewById(R.id.topic_head_netiv);
        imageView = (ImageView) headView.findViewById(R.id.topic_head_open_iv);
        alias = (TextView) headView.findViewById(R.id.topic_head_alias_tv);
        description = (TextView) headView.findViewById(R.id.topic_head_description_tv);
        title = (TextView) headView.findViewById(R.id.topic_head_title_tv);
        concernCount = (TextView) headView.findViewById(R.id.topic_head_concernCount_tv);
        questionCount = (TextView) headView.findViewById(R.id.topic_head_questionCount_tv);
        hot = (TextView) headView.findViewById(R.id.topic_head_hot_btn);
        latest = (TextView) headView.findViewById(R.id.topic_head_latest_btn);
        open= (ImageView) headView.findViewById(R.id.topic_head_open_iv);
        open.setImageResource(R.mipmap.zhankai);

        hot.setOnClickListener(this);
        latest.setOnClickListener(this);
        Intent intent = getIntent();
        String url = intent.getStringExtra("expertId");
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == 101) {
                    beans = (TopicDetailBeans) msg.obj;

                    MyRequestQueue.setNetImage(circleImageView, beans.getData().getExpert().getHeadpicurl());
                    MyRequestQueue.setNetImage(networkImageView, beans.getData().getExpert().getPicurl());
                    networkImageView.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
                    alias.setText(beans.getData().getExpert().getAlias());
                    description.setText(beans.getData().getExpert().getDescription());
                    title.setText(beans.getData().getExpert().getName() + "  |  " + beans.getData().getExpert().getTitle());
                    concernCount.setText("---  " + beans.getData().getExpert().getConcernCount() + "关注  ---");
                    questionCount.setText(beans.getData().getExpert().getQuestionCount() + "提问  " + beans.getData().getExpert().getAnswerCount() + "回复");
                    latest.setBackgroundColor(Color.parseColor("#ff0000"));
                    latest.setTextColor(Color.parseColor("#ffffff"));
                    hot.setBackgroundColor(Color.parseColor("#ffffff"));
                    hot.setTextColor(Color.parseColor("#000000"));
                    imageView.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            if (state) {
                                description.setMaxLines(Integer.MAX_VALUE);
                                open.setImageResource(R.mipmap.guanbi);
                                state=false;

                            } else {
                                description.setMaxLines(2);
                                open.setImageResource(R.mipmap.zhankai);
                                state=true;
                            }
                        }
                    });

                    listView.addHeaderView(headView);
                    adapter = new TopicDetailLatestAdapter(getBaseContext(), beans);
                    listView.setAdapter(adapter);
                }
                return false;
            }
        });
        MyRequestQueue.getBeans(TopicDetailBeans.class, getUrl(url), handler);

    }

    private String getUrl(String u) {
        u = "http://c.m.163.com/newstopic/qa/" + u + ".html";
        return u;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.topic_head_hot_btn:
                hot.setBackgroundColor(Color.parseColor("#ff0000"));
                hot.setTextColor(Color.parseColor("#ffffff"));
                latest.setBackgroundColor(Color.parseColor("#ffffff"));
                latest.setTextColor(Color.parseColor("#000000"));
                adapter.chageState(1);
                break;
            case R.id.topic_head_latest_btn:
                latest.setBackgroundColor(Color.parseColor("#ff0000"));
                latest.setTextColor(Color.parseColor("#ffffff"));
                hot.setBackgroundColor(Color.parseColor("#ffffff"));
                hot.setTextColor(Color.parseColor("#000000"));
                adapter.chageState(0);
                break;
        }
    }


}
